package com.qiuhui.entity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

/**
 * @ClassName: JsonBase
 * @Description: TODO
 * @author: tian
 * @date: 2021/3/22  7:40 下午
 */
public class JsonBase {
    public static void main(String args[]) {
        // 对象 --> json字符串
        Blog blog = new Blog("cat", "springMVC", 800000);
        String str = JSON.toJSONString(blog);
        System.out.println("对象转字符串：" + str);

        // json字符串 --> 对象
        Blog blog1 = JSON.parseObject(str, Blog.class);
        System.out.println("字符串转对象：" + blog1);

        // JAVA Object --> JSONObject
        JSONObject object = (JSONObject) JSON.toJSON(blog);
        System.out.println(object);

        // JSONObject --> JAVA Object
        Blog blog2 = JSON.toJavaObject(object, Blog.class);
        System.out.println(blog2);
        /**
         * 对象转字符串：{"author":"cat","title":"springMVC","view":800000}
         * 字符串转对象：Blog(author=cat, title=springMVC, view=800000)
         * {"view":800000,"author":"cat","title":"springMVC"}
         * Blog(author=cat, title=springMVC, view=800000)
         */
    }
}
